const { get, post } = require('../../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNubmer: 1,
    bankAccountId: '',
    moneyRecords: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  onChangeHead({ detail: { activeCard:
    { bankAccount: { id } } } }) {
    this.setData({
      bankAccountId: id,
      pageNubmer: 1,
    })

    this.getPageData(id);
  },
  getPageData(bankAccountId) {
    const accountId = bankAccountId ? bankAccountId : this.data.bankAccountId;
    const monthNumber = this.data.choosedDate;
    const pageNumber = bankAccountId ? 1 : this.data.pageNubmer;
    const startIndex = bankAccountId ? 0 : this.data.moneyRecords.length;
    const dataLength = bankAccountId ? this.data.moneyRecords.length : 0;

    get(`lendrecord/page/${pageNumber}`, {
      "filter_bankAccount.id_eq_i": accountId,
      "length": 6
    }, ({ datas }) => {
      if (datas && datas.length == 0) {
        if (pageNumber == 1) {
          this.setData({
            moneyRecords: []
          })
          this.applyDataUpdates()
          return
        }
        return;
      }

      datas.forEach(item => {
        item.createTimeStr = item.createTimeStr.substring(0, 10)
      })
      this.setData({
        pageNubmer: this.data.pageNubmer + 1
      })

      this.spliceArrayDataOnPath(['moneyRecords'], startIndex, dataLength, datas);
      this.applyDataUpdates()
    });
  },
  onClickItemRecord({ currentTarget: { id } }) {
    const dataId = this.data.moneyRecords[id].id;
    wx.navigateTo({
      url: `/pages/dataAnalysis/lendRecord/lendRecordDetail/lendRecordDetail?id=${dataId}`
    })
  },
  onScrollToLower() {
    this.getPageData();
  },


})